<?php

namespace app\modules\admin\models;
use app\components\BaseViewList;
use app\models\User;
/**
 * Description of ViewError
 *
 * @author shuai
 */
class ViewOrder extends \app\components\BaseViewList {

    public $zid = null;

    /**
     * 表格上面的内容
     */
    public function tophtml() {
        return '';
    }

    public function get_model() {

        return new \app\models\Order();
    }

    public $show_add = false; ///显示添加按钮

    /**
     * 获取表格的唯一名字
     */

    public function getName() {
        return 'Order';
    }

    /**
     * 是否显示删除按钮
     */
    public $show_delete = false;

    /**
     * 是否显示复选框
     */
    // public $show_select_box=false;
    //  public $auto_detection=0;

    /**
     * 需要继承,然后定义每行需要显示的数据格式
     */
    public function Labels() {
        return array(
            'id' => array('style' => 'width:50px'),
            'time' => '',
            'user_id' => '',
            'username'=>'',
            'ogp_trade_no' => '',
            'cp_trade_no' => '',
            'trade_code' => '',
            'paytime' => '',
            'user_id' => '',
            'total_fee' => '',
            'total_feew' => '',
                /* 'control' => [
                  'edit',
                  //'delete'
                  ] */
        );
    }

    /**
     * 方便继承,多一层过滤
     * 这里过滤一下时间
     */
    public function ModelTohtml($key, $model) {
        if ($key == 'time' || $key == 'paytime') {
            return $this->toTime($model->$key);
        }
        if ($key == 'total_fee' || $key == 'total_feew') {
            return $this->total_fee($model->$key);
        }
        if ($key == 'trade_code') {
            return \app\models\Order::codeTostr($model->trade_code);
        }
        if($key=='username'){
            return $this->tousername($model->user_id);
        }
        return $model->$key;
    }

    private function total_fee($total) {
        return $total / 100;
    }
    private function tousername($userid){
        return User::OpenidToName($userid);
    }

    /**
     * 顶部的选择区,参数,如果为空,则不显示选择器
     */
    public function getselectAttr() {
        $where = [
            'user_id' =>
            [
                'title' => '用户id',
                'inputtype' => BaseViewList::htmlText, 
            ],
            'trade_code' =>
            [
                'title' => '订单状态', 
                'inputtype' => BaseViewList::dropDownList,
                'items' => ['all'=>'所有']+\app\models\Order::codeTostr('all'),
                'default' => 'all'
            ]
        ];
        return $where;
    }
    
  /**
     * 返回查询的条件
     */
    public function find() {
        $find = $this->get_model()->find();

        if (isset($_GET['submit']) && $_GET['submit'] == 'true') {
            $this->andWhere($find);
        }

        $find->orderBy($this->get_primaryKey() . ' DESC');
        if(isset($_GET['user_id'])&&$_GET['user_id']!==''){
             $find->andWhere('user_id = :user_id',[':user_id'=>$_GET['user_id']]);
        }
       if(isset($_GET['trade_code'])&&$_GET['trade_code']!==''&&$_GET['trade_code']!=='all'){
             $find->andWhere('trade_code LIKE :trade_code',[':trade_code'=>$_GET['trade_code']]);
        }
        
        return $find;
    }

    /**
     * 对话框中的各个字段
     */
    public function dialog() {

        return array(
        );
    }

}
